package com.geilizu.dom.controller;

import com.geilizu.dom.dto.SearchDto;
import com.geilizu.dom.entity.Building;
import com.geilizu.dom.entity.Dormitory;
import com.geilizu.dom.result.ResponseCode;
import com.geilizu.dom.result.ResponseData;
import com.geilizu.dom.service.DormitoryService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Dormitory)表控制层
 *
 * @author makejava
 * @since 2022-07-12 17:27:05
 */
@RestController
@RequestMapping("dormitory")
public class DormitoryController {
    @Resource
    private DormitoryService dormitoryService;

    @GetMapping("/availableList")
    public ResponseData availableList(){
        List<Dormitory> dormitoryList = this.dormitoryService.queryAll();
        return new ResponseData(ResponseCode.SUCCESS,dormitoryList);
    }
    @PostMapping("/save")
    public ResponseData save(@RequestBody Dormitory dormitory) {
        Integer i = dormitoryService.save(dormitory);
        if (i > 0) {
            return new ResponseData(ResponseCode.SUCCESS);
        }
        return new ResponseData(ResponseCode.FAILED);
    }


    @GetMapping("/listDo/{page}/{size}")
    public ResponseData listDo(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Map<String, Object> map = dormitoryService.listDo(page, size);
        return new ResponseData("200", "success", (Long) map.get("count"), map.get("dormitoryList"));
    }

    @PostMapping("/search")
    public ResponseData search(@RequestBody SearchDto searchDto) {
//        System.out.println(searchDto.getKey());
        Map<String, Object> map = dormitoryService.search(searchDto);
        return new ResponseData("200", "success", (Long) map.get("count"), map.get("dormitoryList"));
    }

    @GetMapping("/findById/{id}")
    public ResponseData findById(@PathVariable("id") Integer id){
        return new ResponseData(ResponseCode.SUCCESS,dormitoryService.findById(id));
    }

    @PutMapping("/update")
    public ResponseData update(@RequestBody Dormitory dormitory){
        Integer update = this.dormitoryService.updateById(dormitory);
        if(update>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }else{
            return new ResponseData(ResponseCode.FAILED);
        }

    }
}

